import { defineStore } from 'pinia'
// import { reqLogin, reqCaInfo } from '@/api/user'
// import type { loginForm, loginResponseData, CategoryResponseData } from '@/api/user/type'
import { toRaw } from 'vue'
import { fnAddDynamicMenuRoutes,existRouteComponent } from '@/utils/dynamicRoutes'
// import { SET_TOKEN } from '@/utils/token'
export const usePermissionStore = defineStore('Permission', {
  // :PermissionState
  // state: ():UserState => {
  //   return {
  //     token: '12',
  //     menuRoutes:productRoutes//存储生成菜单需要数组（路由）
  //   }
  // },
  state:() => {
    return {
      token: '',
      sidebarMenuList: [
        {
          createdTime: '2020-05-07 23:45:43',
          updatedTime: '2020-05-28 23:10:38',
          deleted: 0,
          id: 9,
          tenantId: null,
          menuName: '系统管理',
          menuEnName: '',
          menuPid: 1,
          menuOrder: 5,
          menuIcon: 'Setting',
          menuFlag: null,
          isVisible: 1,
          menuStatus: 1,
          title: '系统管理',
          routerName: 'sysMgmt',
          routerPath: 'sysMgmt',
          descriptions: 'abstract',
          modelId: null,
          isLeaf: 0,
          childMenus: [
            {
              createdTime: '2020-05-07 23:56:06',
              updatedTime: '2020-10-21 06:30:41',
              deleted: 0,
              id: 5,
              tenantId: null,
              menuName: '用户管理',
              menuEnName: '',
              menuPid: 9,
              menuOrder: 2,
              menuIcon: 'User',
              menuFlag: null,
              isVisible: 1,
              menuStatus: 1,
              title: '用户管理',
              routerName: 'user',
              routerPath: 'sysMgmt/user',
              descriptions: '',
              modelId: null,
              isLeaf: 0,
              childMenus: []
            },
            {
              createdTime: '2020-05-07 23:57:47',
              updatedTime: '2020-05-09 03:58:18',
              deleted: 0,
              id: 6,
              tenantId: null,
              menuName: '角色管理',
              menuEnName: '',
              menuPid: 9,
              menuOrder: 2,
              menuIcon: 'CreditCard',
              menuFlag: null,
              isVisible: 1,
              menuStatus: 1,
              title: '角色管理',
              routerName: 'role',
              routerPath: 'sysMgmt/role',
              descriptions: '',
              modelId: null,
              isLeaf: 0,
              childMenus: []
            }
          ]
        },
        {
          createdTime: '2020-05-01 04:36:52',
          updatedTime: '2020-05-20 06:26:38',
          deleted: 0,
          id: 2,
          tenantId: null,
          menuName: '首页',
          menuEnName: '',
          menuPid: 1,
          menuOrder: 1,
          menuIcon: 'House',
          menuFlag: null,
          isVisible: 1,
          menuStatus: 1,
          title: '首页',
          routerName: 'home',
          routerPath: 'home',
          descriptions: '',
          modelId: null,
          isLeaf: 0,
          childMenus: []
        },
        
      ],
      mixSidebarMenuList:[]
    }
  },
  getters: {},
  actions: {
    updateMixSidebarMenuList(val){
      this.mixSidebarMenuList = val
    },
    handleLogin(){
      this.token = '1212'
        console.log(this.token, 'token')
        existRouteComponent(toRaw(this.sidebarMenuList))
        fnAddDynamicMenuRoutes(toRaw(this.sidebarMenuList))
        return Promise.resolve(this.token)
    },
    // async handleLogin(params: loginForm) {
    //   const result: loginResponseData = await reqLogin(params)
    //   if (result.code === 200) {
    //     //SET_TOKEN(result.data.token as string)
    //     // this.token = result.data.token as string //断言
    //     this.token = '1212'
    //     console.log(this.token, 'token')
    //     existRouteComponent(toRaw(this.sidebarMenuList))
    //     fnAddDynamicMenuRoutes(toRaw(this.sidebarMenuList))
    //     return Promise.resolve(result)
    //   } else {
    //     return Promise.reject(new Error(result.data.message))
    //   }
    // },
    async getMenuList() {
      //刷新浏览器的时候，请求后台接口，获取新的菜单
      existRouteComponent(toRaw(this.sidebarMenuList))
      fnAddDynamicMenuRoutes(toRaw(this.sidebarMenuList))
      return Promise.resolve()
    },
  },
  persist: true
})
